//获取日期
Date.prototype.format = function(pra) {
	var s = '';
	var mouth = (this.getMonth() + 1) >= 10 ? (this.getMonth() + 1)
			: ('0' + (this.getMonth() + 1));
	var day = this.getDate() >= 10 ? this.getDate() : ('0' + this.getDate());
	s += this.getFullYear() + '-'; // 获取年份。
	s += mouth + "-"; // 获取月份。
	if (pra == "01") {
		s += "01";
	} else {
		s += day; // 获取日。
	}

	return (s); // 返回日期。
};

// 获取指定日期间的所有日期
function getBetweenDay(begin, end) {
	var betweenTimeArr = new Array();
	var ab = begin.split('-');
	var ae = end.split('-');
	var db = new Date();
	db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
	var de = new Date();
	de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
	var unixDb = db.getTime();
	var unixDe = de.getTime();
	for (var k = unixDb; k <= unixDe;) {
		betweenTimeArr.push(new Date(parseInt(k)).format());
		k = k + 24 * 60 * 60 * 1000;
	}
	return betweenTimeArr;
}

//获取两日期间天数 日期格式（yyyy-mm-dd）
function getBetweenDay2(begin, end) {	
	var s1 = new Date(begin.replace(/-/g, "/"));
	var s2 = new Date(end.replace(/-/g, "/"));
	var days = s2.getTime() - s1.getTime();
	return parseInt(days / (1000 * 60 * 60 * 24));
}

//获取指定日期 每15分的时间
function getOneDayEach15Min(time) {
	var betweenTimeArr = new Array();
	var year = time.split('-')[0];
	var mon = time.split('-')[1] - 1;
	var day = time.split('-')[2];
	var selectDay = new Date(new Date(year, mon, day)).setHours(0, 0, 0, 0);
	
	for (var i = 0; i < 4 * 24; i++) {//一天4 * 24 个 15分
		selectTime = new Date(selectDay);
		 var time = [bitsAdd0(selectTime.getHours()), bitsAdd0(selectTime.getMinutes())].join(':');
		 betweenTimeArr.push(time);
		 selectDay += 15*60 * 1000;   
	}
	
	
	return betweenTimeArr;
}

// 指定日日期后一天
function getNextDay(d) {
	d = new Date(d);
	d = +d + 1000 * 60 * 60 * 24;
	d = new Date(d);

	var mouth = (d.getMonth() + 1) >= 10 ? (d.getMonth() + 1) : ('0' + (d
			.getMonth() + 1));
	var day = d.getDate() >= 10 ? d.getDate() : ('0' + d.getDate());

	return d.getFullYear() + "-" + mouth + "-" + day;

}

//指定日期前或者后几天
function AddDays(a,num){
	var s1 = new Date(a.replace(/-/g, "/"));	 
	var s2 = s1.getTime() +num* (1000 * 60 * 60 * 24);
	return new Date(s2).format("yyyy-mm-dd");;
	
}

function bitsAdd0(d) {

	return d >= 10 ? d : ('0' + d);

}



// 获取时间yyyy-mm-dd hh:mm:ss
function getNowFormatDate(incomingTime) {
	var date = new Date(incomingTime);
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1
			+ strDate + " " + bitsAdd0(date.getHours()) + seperator2 + bitsAdd0(date.getMinutes())
			+ seperator2 + bitsAdd0(date.getSeconds());
	return currentdate;
}

// 获取日期 yyyy-mm-dd date =new Date();
function getNowFormatTime(incomingTime) {
	var date = new Date(incomingTime);
	var seperator1 = "-";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1
			+ strDate;
	return currentdate;
}

// 转小数
function toPoint(percent) {
	var str = percent.replace("%", "");
	str = str / 100;
	return str;
}
// 转百分数
function toPercent(point) {
	 
	var str = Number(point * 100).toFixed(1);
	if(str==100&&point<1){
		str = 99.9		
	}
	str += "%";
	return str;
}

// G M K 转换为K
function toNumber(disk) {
	disk = disk.toUpperCase();
	if (disk.indexOf('K') > -1) {
		return parseInt(disk.replace("K", ""));

	} else if (disk.indexOf('M') > -1) {
		return parseInt(disk.replace("M", "")) * 1024;

	} else if (disk.indexOf('G') > -1) {
		return parseInt(disk.replace("G", "")) * 1024 * 1024;
	}else{
		
		return 0;
	}
}

// 获取URL地址上的参数
function GetQueryString(name) {	
	var url = decodeURI(decodeURI(window.location.search));	
 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = url.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}

// 获取当前月的第一天
function getCurrentMonthFirst() {
	var date = new Date();
	date.setDate(1);
	return date;
}
// 获取当前月的最后一天
function getCurrentMonthLast() {
	var date = new Date();
	var currentMonth = date.getMonth();
	var nextMonth = ++currentMonth;
	var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
	var oneDay = 1000 * 60 * 60 * 24;
	return new Date(nextMonthFirstDay - oneDay);
}

// KB/S MB/S B/S 转换为以B/S 单位的数
function toThroughputNumber(throughput) {
	throughput = throughput.toUpperCase();
	if (throughput.indexOf('KB/S') > -1) {
		return parseInt(throughput.replace("KB/S", "")) * 1024;

	} else if (throughput.indexOf('MB/S') > -1) {
		return parseInt(throughput.replace("MB/S", "")) * 1024 * 1024;

	} else if (throughput.indexOf('B/S') > -1) {
		return parseInt(throughput.replace("B/S", ""));
	}else{
		
		return 0;
	}
}

//流量单位转换
function fluxUnitConversion(flowSize){
	 
	if(flowSize<1024){//B		
		return flowSize+' B';	
		
	}else if(flowSize<1024*1024){//KB		
		return (flowSize/1024).toFixed(2)+' KB';	
		
	}else if(flowSize<1024*1024*1024){//MB
		return (flowSize/(1024*1024)).toFixed(2)+' MB';
		
	}else if(flowSize<1024*1024*1024*1024){//GB
		return (flowSize/(1024*1024*1024)).toFixed(2)+' GB';
		
	}else if(flowSize<1024*1024*1024*1024*1024){//TB
		return (flowSize/(1024*1024*1024*1024)).toFixed(2)+' TB';		
	}else{
		return '0 B';
	}
} 


 
///集合取交集Array.intersect(ar1,ar2) 
Array.intersect = function () {
    var result = new Array();
    var obj = {};
    for (var i = 0; i < arguments.length; i++) {
        for (var j = 0; j < arguments[i].length; j++) {
            var str = arguments[i][j];
            if (!obj[str]) {
                obj[str] = 1;
            }
            else {
                obj[str]++;
                if (obj[str] == arguments.length)
                {
                    result.push(str);
                }
            }//end else
        }//end for j
    }//end for i
    return result;
};

//集合去掉重复 ar.uniquelize
Array.prototype.uniquelize = function () {
    var tmp = {},
        ret = [];
    for (var i = 0, j = this.length; i < j; i++) {
        if (!tmp[this[i]]) {
            tmp[this[i]] = 1;
            ret.push(this[i]);
        }
    }

    return ret;
};
//并集  Array.union(ar1,ar2,ar3) 
Array.union = function () {
    var arr = new Array();
    var obj = {};
    for (var i = 0; i < arguments.length; i++) {
        for (var j = 0; j < arguments[i].length; j++)
        {
            var str=arguments[i][j];
            if (!obj[str])
            {
                obj[str] = 1;
                arr.push(str);
            }
        }
    }
    return arr;
};

//2个集合的差集 在arr不存在  ar1.minus(arr)
Array.prototype.minus = function (arr) {
    var result = new Array();
    var obj = {};
    for (var i = 0; i < arr.length; i++) {
        obj[arr[i]] = 1;
    }
    for (var j = 0; j < this.length; j++) {
        if (!obj[this[j]])
        {
            obj[this[j]] = 1;
            result.push(this[j]);
        }
    }
    return result;
};

//json 数组去重
function uniqueArray(array, key){
  var result = [array[0]];
  for(var i = 1; i < array.length; i++){
      var item = array[i];
      var repeat = false;
      for (var j = 0; j < result.length; j++) {
          if (item[key] == result[j][key]) {
              repeat = true;
              break;
          }
      }
      if (!repeat) {
          result.push(item);
      }
  }
  return result;
}

//服务类型
function getServType(typeNum){
	
	var result1= '请求响应';
	var result2= '发布订阅-发布方';
	var result3= '发布订阅-订阅方';
	var i18n= getCookie("org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE");
	
	if(i18n=="en_US"){
		var result1= 'Request-Response';
		var result2= 'Publisher Service';
		var result3= 'Subscriber Service';
	}
	
	if(typeNum=='0'){
		return result1;
		
	}else if(typeNum=='1'){
		
		return result2;
	}else if(typeNum=='2'){
		
		return result3;
	}else{
		
		return ''; 
	}
}

/* var employees=[]
 * employees[0]={name:"George", age:32, retiredate:"2034"}
 * employees[1]={name:"Edward", age:32, retiredate:"2023"}
 * employees[2]={name:"Christine", age:32, retiredate:"2036"}
 * employees[3]={name:"Sarah", age:62, retiredate:"2020"}
 * 按照age和retiredate降序
 * employees.sort(by("age",by("retiredate",null,"desc"),"desc"));
 * */
var by = function(name,minor,order){
 return function(o,p){
   var a,b;
   if(o && p && typeof o === 'object' && typeof p ==='object'){
     a = o[name];
     b = p[name];
     if(a === b){
       return typeof minor === 'function' ? minor(o,p):0;
     }
     if(typeof a === typeof b){
	     if(order=="desc"){
	     	return a < b ? 1:-1;
	     }else{
	     	return a < b ? -1:1;	
	     }       
     }
      if(order=="desc"){
	     	 return typeof a < typeof b ? 1:-1;
	     }else{
	     	 return typeof a < typeof b ? -1 : 1;	
	     } 
     
    
   }else{
     thro("error");
   }
 }
}



